﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data = DeltaFlex.Data;
using System.Data;
using Taula = DeltaFlex.Negoci.ConsTaules.Client;

namespace DeltaFlex.Negoci
{
    /// <summary>
    /// Definició dels atributs i dels mètdes d'accés a BD de l'objecte Client
    /// </summary>
    [Serializable()]
    public class Client
    {
        #region Variables
        /// <summary>
        /// Definició de les variables globals de l'objecte Client
        /// </summary>
        private int _idClient;
        private string _nomClient;
        private string _nifClient;
        private int _idAdreçaClient;
        private int _telefonClient;
        private int _faxClient;
        private string _email;
        private string _numCompte;
        private string _formaPago;
        private AdreçaClient _adreçaClient;
        #endregion

        #region Atributs
        /// <summary>
        /// Definició dels atributs de l'objecte
        /// </summary>
        public int IdClient
        {
            get { return _idClient; }
        }
        public string NomClient
        {
            get { return _nomClient; }
            set { _nomClient = value; }
        }
        public string NifClient
        {
            get { return _nifClient; }
            set { _nifClient = value; }
        }
        
        public int IdAdreçaClient
        {
            get { return _idAdreçaClient; }
            set { _idAdreçaClient = value; }
        }
        public int TelefonClient
        {
            get { return _telefonClient; }
            set { _telefonClient = value; }
        }
        public int FaxClient
        {
            get { return _faxClient; }
            set { _faxClient = value; }
        }
        public string Tipus
        {
            get { return _email; }
            set { _email = value; }
        }

        public string NumCompte
        {
            get { return _numCompte; }
            set { _numCompte = value; }
        }

        public string FormaPago
        {
            get { return _formaPago; }
            set { _formaPago = value; }
        }

        public AdreçaClient AdreçaClient
        {
            get { return _adreçaClient; }
            set { _adreçaClient = value; }
        }
        #endregion

        public Client()
        {

        }

        #region Inserts
        /// <summary>
        /// Crida a l'objecte Model.Client
        /// </summary>
        /// <returns>Retorna si ha estat possible afegir un nou registre a la BD</returns>
        public static int InsertarClient(string nomClient, string nifClient, int telefonClient, int faxClient, string email, string numCompte,string formaPago,
                                         string carrerNumero, string poligon, string ciutat, int codiPostal, string provincia, string pais)
        {
            try
            {
                return Data.Client.InsertarClient(nomClient, nifClient, telefonClient, faxClient, email, numCompte, formaPago,
                                                  carrerNumero, poligon, ciutat, codiPostal,provincia,pais);
            }
            catch { throw; }
        }

        #endregion

        #region Selects
        public static List<Client> LlistarClientByIDClient(int idClient)
        {
            List<Client> lstClient = new List<Client>();
            IDataReader reader = null;

            try
            {
                reader = Data.Client.LlistarClientsByIDClient(idClient);

                while (reader.Read())
                {
                    lstClient.Add(MapEntityFromDataReader(reader));
                }
            }
            catch { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }

            return lstClient;
        }
        #endregion

        #region Mapeig
        public static Client MapEntityFromDataReader(IDataReader record)
        {
            Client client = new Client();

            MemberAllocation.Allocate(record, Taula.COLUMNA_IDCLIENT, ref  client._idClient);
            MemberAllocation.Allocate(record, Taula.COLUMNA_NOMCLIENT, ref  client._nomClient);
            MemberAllocation.Allocate(record, Taula.COLUMNA_NIFCLIENT, ref  client._nifClient);
            MemberAllocation.Allocate(record, Taula.COLUMNA_IDADREÇACLIENT, ref  client._idAdreçaClient);
            MemberAllocation.Allocate(record, Taula.COLUMNA_TELEFONCLIENT, ref  client._telefonClient);
            MemberAllocation.Allocate(record, Taula.COLUMNA_FAXCLIENT, ref  client._faxClient);
            MemberAllocation.Allocate(record, Taula.COLUMNA_EMAIL, ref  client._email);
            MemberAllocation.Allocate(record, Taula.COLUMNA_NUMCOMPTE, ref  client._numCompte);
            MemberAllocation.Allocate(record, Taula.COLUMNA_FORMAPAGO, ref  client._formaPago);

            client._adreçaClient = AdreçaClient.MapEntityFromDataReader(record);

            return client;
        }
        
        public static Client MapEntityFromId(IDataReader record)
        {
            Client client = new Client();

            MemberAllocation.Allocate(record, Taula.COLUMNA_IDCLIENT, ref  client._idClient);

            return LlistarClientByIDClient(client._idClient)[0];
        }
        #endregion
    }

}

